package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.auth.annotation.HasPermissions;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.log.annotation.OperLog;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.utils.ToolUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.service.ISysInsuranceweekDetailsService;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 投保周明细下载 提供者
 * 
 * @author ruoyi
 * @date 2021-09-17
 */
@Api(value = "投保周明细下载")
@RestController
@RequestMapping("sysInsuranceweekDetails")
public class SysInsuranceweekDetailsController extends BaseController
{
	
	@Autowired
	private ISysInsuranceweekDetailsService sysInsuranceweekDetailsService;
	@Autowired
	private SysBusinessInfoMapper sysBusinessInfoMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysDeptMapper sysDeptMapper;
	@Autowired
	private SysDealerInfoMapper sysDealerInfoMapper;
	@Autowired
	private SysInsuranceDetailsMapper sysInsuranceDetailsMapper;
	
	/**
	 * 查询${tableComment}
	 */
	@ApiOperation(value = "根据id查询")
	@ApiImplicitParam(name = "id", value = "id", required = true)
	@GetMapping("get/{id}")
	public SysInsuranceweekDetails get(@PathVariable("id") Long id)
	{
		return sysInsuranceweekDetailsService.selectSysInsuranceweekDetailsById(id);
		
	}
	
	/**
	 * 查询投保周明细下载列表
	 */
	@ApiOperation(value = "查询投保周明细下载列表及根据条件查询列表")
	@GetMapping("list")
	public R list(SysInsuranceweekDetails sysInsuranceweekDetails)
	{
		startPage();
        return result(sysInsuranceweekDetailsService.selectSysInsuranceweekDetailsList(sysInsuranceweekDetails));
	}
	
	
	/**
	 * 新增保存投保周明细下载
	 */
	@ApiOperation(value = "新增保存投保周明细下载")
	@PostMapping("save")
	public R addSave(@RequestBody SysInsuranceweekDetails sysInsuranceweekDetails)
	{		
		return toAjax(sysInsuranceweekDetailsService.insertSysInsuranceweekDetails(sysInsuranceweekDetails));
	}

	/**
	 * 修改保存投保周明细下载
	 */
	@ApiOperation(value = "修改保存投保周明细下载")
	@PostMapping("update")
	public R editSave(@RequestBody SysInsuranceweekDetails sysInsuranceweekDetails)
	{		
		return toAjax(sysInsuranceweekDetailsService.updateSysInsuranceweekDetails(sysInsuranceweekDetails));
	}
	
	/**
	 * 删除${tableComment}
	 */
	@ApiOperation(value = "删除")
	@ApiImplicitParam(name = "ids", value = "ids", required = true)
	@PostMapping("remove")
	public R remove(String ids)
	{		
		return toAjax(sysInsuranceweekDetailsService.deleteSysInsuranceweekDetailsByIds(ids));
	}

	@ApiOperation(value = "导出投保周明细execl")
	@ApiImplicitParam(name = "ids", value = "ids", required = true)
	@OperLog(title = "投保周明细", businessType = BusinessType.EXPORT)
	@HasPermissions("system:sysInsuranceweekDetailsList:export")
	@PostMapping("/export")
	public R export(String ids)
	{
		String name = "";
		try{
			List<SysBusinessInfo> sysBusinessInfoList = sysBusinessInfoMapper.selectInsuranceweekDetailsId(Long.valueOf(ids));
			if(sysBusinessInfoList != null && !sysBusinessInfoList.isEmpty()){
				SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(sysBusinessInfoList.get(0).getCreateBy()));
				String organCode = "";
				String dealerName = "";
				if(null != sysUser){
					SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());
					if(null != sysDept){
						dealerName = sysDept.getDeptName();
						organCode = sysDealerInfoMapper.selectOrganCodeByDealerName(dealerName);
					}
				}

				List<WeekDetailsExport> list = new ArrayList<>();
				name = UUID.randomUUID().toString() + "_" + "投保周明细.xlsx";
				String filePath = ToolUtil.getDownloadPath() + name;
				for(int i=0;i<sysBusinessInfoList.size();i++){
					WeekDetailsExport weekDetailsExport = new WeekDetailsExport();
					weekDetailsExport.setNum(i+1);
					SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
					weekDetailsExport.setOrderTime(df.format(sysBusinessInfoList.get(i).getOrderTime()));
					weekDetailsExport.setCarType(sysBusinessInfoList.get(i).getCarType());
					weekDetailsExport.setServiceStakeholders(sysBusinessInfoList.get(i).getServiceStakeholders());
					weekDetailsExport.setCarNumberAndEngineNo(sysBusinessInfoList.get(i).getCarNumber());
					weekDetailsExport.setVinNo(sysBusinessInfoList.get(i).getVinNo());
					weekDetailsExport.setCarBrand(sysBusinessInfoList.get(i).getCarBrand());
					weekDetailsExport.setInsurancePremium(sysBusinessInfoList.get(i).getInsurancePremium().doubleValue());
					// 保险止期，即保险起期+1年-1天

					SysInsuranceDetails sysInsuranceDetails = sysInsuranceDetailsMapper.selectSysInsuranceDetailsById(sysBusinessInfoList.get(i).getInsuranceDetailsId());
					if(null != sysInsuranceDetails){
						Date date = df.parse(sysInsuranceDetails.getInsuranceStartdate());
						Calendar cal = Calendar.getInstance();
						cal.setTime(date);
						cal.add(Calendar.YEAR, 1);
						cal.add(Calendar.DATE, 1);
						weekDetailsExport.setInsuranceStartdate(df.format(sysInsuranceDetails.getInsuranceStartdate()));
						weekDetailsExport.setInsurance(df.format(cal.getTime()));
					}
					weekDetailsExport.setDealerName(dealerName);
					weekDetailsExport.setOrganCode(organCode);
					weekDetailsExport.setFilePath(filePath);
					list.add(weekDetailsExport);
				}

				JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
				HttpUtils.sendPost("http://127.0.0.1:8080/test",array.toJSONString());
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return R.ok(name);
	}
}
